//
//  MovieDataViewController.m
//  豆瓣2
//
//  Created by dllo on 15/12/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieDataViewController.h"
#import "MovieDataView.h"
#import "AppTools.h"
#import "MovieData.h"
#import "UIImageView+WebCache.h"
#import "MtViewController.h"
#import "DataBaseSingleton.h"

@interface MovieDataViewController ()

@property (nonatomic ,retain)NSMutableArray *allArray;
@property (nonatomic ,retain)MovieDataView *myView;
@property (nonatomic ,retain)MovieData *data;
@end

@implementation MovieDataViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myView = [[MovieDataView alloc]initWithFrame:self.view.bounds];
    self.myView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myView];
    self.allArray = [NSMutableArray array];
    
    NSString *str = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.movie.movieId];
    [AppTools getMovieFromJsonWithfilmName:str block:^(id result) {
        
               NSMutableDictionary *dic = [result objectForKey:@"result"];
        
            self.data = [[MovieData alloc]init];
            
            [self.data setValuesForKeysWithDictionary:dic];
            
            [self.allArray addObject:self.data];
        
        self.myView.rating.text = self.data.rating;
        self.myView.time.text = self.data.release_date;
        self.myView.elease.text = self.data.runtime;
        self.myView.genres.text = self.data.genres;
        self.myView.county.text = self.data.country;
        self.myView.zlab.text = self.data.actors;
        self.myView.film.text = self.data.plot_simple;
        self.navigationItem.title = self.data.title;
        NSURL *url = [NSURL URLWithString:self.data.poster];
        [self.myView.myView sd_setImageWithURL:url placeholderImage:nil];
        
        
        CGFloat height = [MovieDataViewController shareSubView:self.data.plot_simple];
        CGRect frame = self.myView.film.frame;
        frame.size.height = height;
        self.myView.film.frame = frame;
        
        self.myView.scrollView.contentSize = CGSizeMake(0,370+100+height);
        

    } error:^(NSError *error) {
        
        
    }];
    
 // 收藏按钮
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_share@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(storeButtonAction:)];
    
   
}

- (void)storeButtonAction:(UIBarButtonItem *)storebnt{
    
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"bool"];
    
    if (str == nil) {
        
        MtViewController *mtView = [[MtViewController alloc] init];
        
        [[NSUserDefaults standardUserDefaults]setObject:@"no" forKey:@"bool"];
        [self presentViewController:mtView animated:YES completion:^{
            
            
        }];
        
        
    }else{
        NSMutableArray *arrayOne = [NSMutableArray array];
    NSMutableArray *array = [[DataBaseSingleton shareDataBase]selectUser];
        
        for (Movie *movie in array) {
            
            [arrayOne addObject:movie.movieName];
        }
        
        if ([arrayOne containsObject:self.movie.movieName]) {
            
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aler animated:YES completion:^{
                
                
            }];
            
            UIAlertAction *bler = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [aler addAction:bler];
        }else{
            
            [[DataBaseSingleton shareDataBase]inserUserIntoTable:self.movie];
            
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aler animated:YES completion:^{
                
                
            }];
            
            UIAlertAction *bler = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [aler addAction:bler];

        }
    }
}

+(CGFloat)shareSubView:(NSString *)str{
    
    CGSize size = CGSizeMake(414-60, 10000);
    
 CGRect frame = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0],NSFontAttributeName, nil] context:nil];
    
    return frame.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
